﻿<template>
  <l-layout class="l-tab-page" v-if="!lr_loadPage">
    <l-panel title="单位列表">
      <template #toolLeft>
        <l-query2
          :items="queryItems"
          :formData="queryData"
          :loading="tableLoading"
          @search="handleSearch"
        >
          <template #f_UnitTypeId>
            <l-select
              :options="f_UnitTypeIdOptions"
              v-model="queryData.f_UnitTypeId"
            />
          </template>
          <template #f_IsStandard>
            <l-select
              placeholder="请选择"
              :options="isStandardOptions"
              v-model="queryData.f_IsStandard"
              labelKey="label"
              valueKey="value"
            />
          </template>
          <template #keyword>
            <el-input
              v-model="queryData.keyword"
              :placeholder="$t('请输入关键字')"
            />
          </template>
        </l-query2>
      </template>
      <template #toolRight>
        <l-tool-btns @setting="lr_handleSettingTable" @click="handleAdd()">
          <l-excel-btns></l-excel-btns>
        </l-tool-btns>
      </template>
      <l-table
        ref="mainTable"
        :loading="tableLoading"
        :columns="lr_getPageColumns(columns)"
        :dataSource="tableData"
        :isPage="true"
        :pageTotal="tableTotal"
        :tablePage.sync="tableCurrentPage"
        @loadPageData="turnTablePage"
      >
        <template
          v-for="(col, index) in lr_tableColumnsByToLabel(
            lr_getPageColumns(columns)
          )"
          v-slot:[col.prop]="scope"
        >
          <l-value-to-label
            :value="scope.row[col.prop]"
            :type="col.dataType"
            :code="col.dataCode"
            :valueKey="col.valueKey"
            :labelKey="col.labelKey"
            :options="col.options"
            :format="col.format"
            :setting="col.setting"
            :key="index"
          ></l-value-to-label>
        </template>
        <template v-slot:f_State="scope">
          <el-switch
            :active-value="0"
            :inactive-value="1"
            v-model="scope.row.f_State"
            @change="changeState(scope.row)"
          >
          </el-switch>
        </template>
        <l-table-btns
          :btns="tableBtns"
          @click="lr_handleTableBtnClick"
        ></l-table-btns>
      </l-table>
    </l-panel>

    <!--表单-->
    <l-dialog
      :title="$t(formTitle)"
      :visible.sync="formVisible"
      :height="300"
      :width="500"
      @ok="handleSave"
      @close="handleCloseForm"
      @opened="handleOpenedForm"
    >
      <my-form ref="form" :isReadOnly="formDetails"></my-form>
    </l-dialog>
  </l-layout>
</template>

<script>
import MyForm from "./form";
const api = window.$api.erpcase.unit;
export default {
  components: {
    MyForm,
  },
  data() {
    return {
      lr_isPage: true,
      moduleCode: "ERP_Units",

      // 查询参数
      queryData: {
        f_UnitTypeId: "",
        f_IsStandard: "",
        keyword: "",
      },

      queryItems: [
        { label: "单位类型", prop: "f_UnitTypeId" },
        { label: "是否基准单位", prop: "f_IsStandard" },
        { label: "关键字", prop: "keyword" },
      ],

      // 单位类型选项组
      UnitTypeOptions: [],

      // 是否基准单位
      isStandardOptions: [
        { value: "0", label: "是" },
        { value: "1", label: "否" },
      ],

      // 表格参数
      tableLoading: false,
      tableData: [],
      tableTotal: 0,
      tableCurrentPage: 1,
      tablePageSize: 50,
      columns: [
        {
          label: "单位类型",
          prop: "f_UnitTypeId",
          minWidth: 120,
          align: "left",
          dataType: "dataSource",
          dataCode: "ERP_UnitTypes",
          valueKey: "f_id",
          labelKey: "f_name",
        },
        {
          label: "单位名称",
          prop: "f_Name",
          minWidth: 120,
          align: "left",
        },
        {
          label: "单位符号",
          prop: "f_Symbol",
          minWidth: 120,
          align: "left",
        },
        {
          label: "是否基准单位",
          prop: "f_IsStandard",
          minWidth: 120,
          align: "left",
          dataType: "mydata",
          options: [
            { value: 0, label: "是" },
            { value: 1, label: "否" },
          ],
        },
        {
          label: "创建用户",
          prop: "f_CreateUserId",
          minWidth: 120,
          align: "left",
          dataType: "user",
        },
        {
          label: "创建日期",
          prop: "f_CreateDate",
          minWidth: 130,
          align: "left",
          dataType: "datetime",
          format: "yyyy-MM-dd HH:mm:ss",
        },
        {
          label: "状态",
          prop: "f_State",
          minWidth: 120,
          align: "left",
          dataType: "input",
        },
      ],
      tableBtns: [
        { prop: "Edit", label: "编辑",width:40 },
        { prop: "Delete", label: "删除",width:40 },
      ],

      // 表单参数
      formVisible: false,
      formTitle: "",
      formEditRow: null,
      formEdit: false,
      formDetails: false,
    };
  },
  computed: {
    f_UnitTypeIdOptions() {
      const data = this.lr_DataSource("ERP_UnitTypes") || [];
      return this.lr_DataSourceOptions(data.filter(t=>t.f_state == 0), "f_id", "f_name");
    },
  },
  mounted() {
    this.init();
  },
  methods: {
    async init() {
      this.loadTableData();
    },

    handleSearch() {
      this.loadTableData();
    },
    turnTablePage({ rows }) {
      this.tablePageSize = rows;
      this.loadTableData(true);
    },
    async loadTableData(isNotFirst) {
      if (!isNotFirst) {
        this.tableCurrentPage = 1;
      }
      this.tableLoading = true;
      let queryData = this.$deepClone(this.queryData);

      queryData.rows = this.tablePageSize;
      queryData.page = this.tableCurrentPage;
      queryData.sidx =
        "F_UnitTypeId DESC,F_IsStandard ASC,F_State ASC,F_CreateDate DESC";
      let data = await this.$awaitWraper(api.getPage(queryData));
      if (data != null) {
        this.tableData = data.rows;
        this.tableTotal = data.records;
      } else {
        this.tableData = [];
      }
      this.tableLoading = false;
    },

    handleAdd() {
      this.formEdit = false;
      this.formDetails = false;
      this.handleShowForm("新增单位列表");
    },
    handleEdit($index, row) {
      this.formEdit = true;
      this.formDetails = false;
      this.formEditRow = row;
      this.handleShowForm("编辑单位列表");
    },
    handleDelete($index, row) {
      this.$confirm(
        this.$t("删除该单位，其他使用该单位的功能可能会收到影响，确认删除吗？"),
        this.$t("提示"),
        {
          confirmButtonText: this.$t("确定"),
          cancelButtonText: this.$t("取消"),
          type: "warning",
        }
      )
        .then(async () => {
          this.tableData.splice($index, 1);
          this.tableTotal--;
          await api.remove(row.f_Id);
          this.$message({
            type: "success",
            message: this.$t("删除成功!"),
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: this.$t("已取消删除!"),
          });
        });
    },

    async handleSave(showLoading, hideLoading) {
      showLoading("保存数据中...");
      if (await this.$refs.form.validateForm()) {
        const res = await this.$refs.form.saveForm({
          keyValue: this.formEdit ? this.formEditRow.f_Id : "",
          isEdit: this.formEdit,
        });
        if (res) {
          const msg = this.formEdit ? "更新成功!" : "新增成功!";
          this.$message({
            type: "success",
            message: this.$t(msg),
          });
          this.loadTableData();
          this.formVisible = false;
        }
      }
      hideLoading();
    },

    handleShowForm(text) {
      this.formTitle = text;
      this.formVisible = true;
    },
    async handleOpenedForm(showLoading, hideLoading) {
      if (this.formEdit) {
        showLoading("加载数据中...");
        await this.$refs.form.loadFormData({ keyValue: this.formEditRow.f_Id });
        hideLoading();
      }
    },
    handleCloseForm() {
      this.$refs.form.resetForm();
    },
    // 修改列表页的状态
    async changeState(row) {
      let postData = {
        f_State: row.f_State,
        f_Id: row.f_Id,
      };
      let res = await this.$awaitWraper(api.changeState(row.f_Id, postData));
      if (res) {
        this.$message({
          type: "success",
          message: res,
        });
      }
    },
  },
};
</script>

